<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\rehearse\controller;

use app\common\controller\Admin;
use app\rehearse\controller\api\Simulation as SimulationApi;
use app\rehearse\controller\api\Record as RecordApi;
use app\admin\controller\ajax\Library as Ajax;

/**
 *模拟演练
 * @package app\simulation
 */
class Simulation  extends Admin

{

    protected $api;

    public function _initialize()
    {
        parent::_initialize();
        $this->api = new SimulationApi();
        $this->record = new RecordApi();
        $this->ajax= new Ajax();
    }
    /**
     * 首页
     *
     */
    public function index() {
        $simulationList=collection(model('simulation')->with('user,enterprise,hazard,plan')->where(array('eventType'=>2))->limit(2)->order('startTime desc')->select());
        $this->assign('list',$simulationList);
        $userid=session("user_auth.id");
        $this->assign('userid',$userid);
        $type=$this->ajax->getplanType()->getData();
        $this->assign('type',$type);
        $simulationtype=$this->ajax->getStatusType()->getData();
        $this->assign('simulationtype',$simulationtype);
        $enterpriseList = model('basic/Enterprise')->column('fullName','id');
        $this->assign('enterpriseList',$enterpriseList);
        $startTime=time();
        $this->assign('startTime',$startTime);
        $userList = model('common/user')->column('userName','id');
        $this->assign('userList',$userList);

        return $this->fetch();
    }

    /**
     * 模拟搜索
     */
    public function search($start=null,$end=null,$keyword=null) {
        if(IS_POST){
            $simulationList = $this->api->search(input('start'),input('end'),input('keyword'));
            return $simulationList;
        }
    }

    /**
     * 模拟详情
     */
    public function detail($simulationId) {
        $simulationList = $this->api->simulationList(input('simulationId'))->getData();
        $this->assign('info',$simulationList['data'][0]);
        $type=$this->ajax->getplanType()->getData();
        $this->assign('type',$type);
        $userList = model('common/user')->column('userName','id');
        $this->assign('userList',$userList);
        $enterpriseList = model('basic/Enterprise')->column('fullName','id');
        $this->assign('enterpriseList',$enterpriseList);
        $simulationtype=$this->ajax->getStatusType()->getData();
        $this->assign('status',$simulationtype);

        return $this->fetch();
    }



    /**
     * 添加模拟演练
     *
     */
    public function add() {
        if(IS_POST){
            $data = input('post.');
            $data['eventStatus']=1;
            return $this->api->add($data);
        }
    }


    /**
     * 修改模拟演练
     *
     */
    public function edit($simulationId = null) {

        if(IS_POST){

            return $this->api->edit($data,$data['id']);
        }

        $userid=session("user_auth.id");
        $this->assign('userid',$userid);
        $type=$this->ajax->getplanType()->getData();
        $this->assign('type',$type);
        $simulationtype=$this->ajax->getStatusType()->getData();
        $this->assign('simulationtype',$simulationtype);
        $enterpriseList = model('basic/Enterprise')->column('fullName','id');
        $this->assign('enterpriseList',$enterpriseList);
        $startTime=time();
        $this->assign('startTime',$startTime);
        $userList = model('common/user')->column('userName','id');
        $this->assign('userList',$userList);
        $simulationList = $this->api->simulationList($simulationId)->getData();
        $this->assign('info',$simulationList['data'][0]);
        return $this->fetch();
    }

    /**
     * 删除模拟演练
     */
    public function del($simulationId = null) {
        if(IS_AJAX){
            return $this->api->del($simulationId);
        }
    }



    /**
     * 预案
     */
    public function plan($simulationId = null) {
        if (IS_POST) {
            $pid = input('pid');
            $plan = model('plan')->with('scripts')->where(array('id'=>$pid))->select();
            return json(['code'=>1,'data'=>$plan[0]]);
        }
        $planlist = model('plan')->where(array('natureId'=>2))->select();
        $reservePlanId = input('reservePlanId');
        if (!empty($reservePlanId)) {
        	foreach($planlist as $plan)
            $this->assign('reservePlanId',$reservePlanId);
        }

     
        $this->assign('simulationId',$simulationId);
        $this->assign('planlist',$planlist);
        return $this->fetch();
    }

    /**
     * 设置预案
     */
    public function setPlan($id=null,$reservePlanId=null){
        if (empty($id)&&empty($reservePlanId)) {
            return false;
        }
        $result=model('simulation')->where(array('id'=>$id))->update(['reservePlanId'=>$reservePlanId]);
        $record=[];
        $record['eventId']=$id;
        $record['reservePlanId']=$reservePlanId;
        $record['typeId']=2;
        $record['execTime']=time();
        $record['operationId']=session('user_auth.id');
        $record['updateTime']=time();
        $res=$this->record->add($record)->getData();
        //救援行动表id
        $rid=$res['recordId'];

        //把预案表中的步骤插入行动明细中

        $scripts=model('script')->where(array('reservePlanId'=>$reservePlanId))->select();
        $model=model('Plan/recorddetail');
        foreach ($scripts as $key => $value) {
            $data=[];
            $data['rescueExecId']=$rid;
            $data['actionId']=$value->actionIdNum;
            $data['chargePerson']=$value['chargePerson'];
            $data['actionStatus']=0;
            $data['implementStatus']=0;
            $data['stepDesc']=$value['stepDesc'];
            $data['stepIndex']=$value['stepIndex'];
            $arr[$key]=$data;
        }
        $model->saveAll($arr);

        if ($result&&$res) {
            return json(['code'=>1,'msg'=>'预案选择成功','url'=>url('duty/panorama/index'),'layer'=>true]);
        }else{
            return json(['code'=>0,'msg'=>'预案启动失败,请检查']);
        }


    }

    /**
     * 查看位置
     */
    public function position($dangerSourceId){

        $position=model('basic/dangersource')->where(array('id'=>$dangerSourceId))->field('latitudeLongitude')->find();

        return json(['code'=>1,'position'=>$position['latitudeLongitude']]);

    }

    /**
     * 动态加载
     */
    public function more(){
        $page = input('page');
        if(IS_POST){

            // 获取数据库中所有数据的总数
            $count =model('simulation')->with('user,enterprise,hazard,plan')->where(array('eventType'=>2))->count();

            // 获取前段接受到的总数
            $getCount = $page;

            if($getCount <= $count){
                $code = 1;
                $simulationList=collection(model('simulation')->with('user,enterprise,hazard,plan')->where(array('eventType'=>2))->order('startTime desc')->limit(2*$page)->select());
            }else{
                $code = 1;
                $simulationList=collection(model('simulation')->with('user,enterprise,hazard,plan')->where(array('eventType'=>2))->order('startTime desc')->limit(2*$page)->select());
            }

            return json(['code'=>$code,'data'=>$simulationList]);
        }
    }


    /**
     * 结束演练
     */
    public function over($id=null){


        $result=model('simulation')->where(array('id'=>$id))->update(['eventStatus'=>2]);
        if ($result) {
            return json(['code'=>1,'msg'=>'已结束']);
        }else{
            return json(['code'=>0,'msg'=>'有问题,请检查']);
        }
    }





}



